package apig.client.stud;

import apig.client.*;
import apig.client.login.*;
import apig.client.serviface.*;

import com.google.gwt.event.dom.client.*;
import com.google.gwt.user.client.rpc.*;
import com.google.gwt.user.client.ui.*;

public class StudHeader extends HorizontalPanel {

	private UserData ud;
	
	public StudHeader(UserData ud) {
		this.ud = ud;
		
		setStyleName("studheaderstyle");
		setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		
		HorizontalPanel block = new HorizontalPanel();
		Label l1 = new Label(ud.groupName);
		Label l2 = new Label("[" + ud.userId + "]");
		Label l3 = new Label(ud.name + " " + ud.patronimic);
		l1.setStyleName("apiglabelstyle");
		l2.setStyleName("apiglabelstyle");
		l3.setStyleName("apiglabelstyle");
		block.add(l1);
		block.add(l2);
		block.add(l3);
		Button logoutButton = new Button("Log out");
		logoutButton.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				Stubman.getStubman().getService().doLogout(new AsyncCallback<Boolean>() {					
					@Override
					public void onSuccess(Boolean result) {
						onLogout();
					}
					
					@Override
					public void onFailure(Throwable caught) {
						System.out.println("fail to logout: " + caught.getMessage());
					}
				});
			}});
		Label apigLabel = new Label("Apig project");
		apigLabel.setStyleName("apiglabelstyle");
		Label emptyLabel = new Label(" ");
		
		add(emptyLabel);
		add(apigLabel);
		add(block);
		add(logoutButton);
		
		setCellWidth(emptyLabel, "20%");
		setCellWidth(apigLabel, "40%");
		setCellWidth(block, "25%");
		setCellWidth(logoutButton, "10%");
		
		setCellHorizontalAlignment(apigLabel, HasHorizontalAlignment.ALIGN_CENTER);
		
		setWidth("100%");
	}
	
	private void onLogout() {
		LoginEntryPoint.redirect("http://127.0.0.1:8888/login.html?gwt.codesvr=127.0.0.1:9997");
	}
}
